<?php

namespace App\Model;
use Carbon\Carbon;

/**
 * App\CouponRecord
 *
 * @property int $id
 * @property int $coupon_id
 * @property int $user_id
 * @property-read \App\Model\Coupon $coupon
 * @property-read \App\Model\User $user
 * @property int|null $device_id
 * @property int|null $station_id
 */
class CouponRecord extends Model
{

    public bool $timestamps = true;
    protected ?string $table = 'coupon_records';
    //
//    protected array $casts = [
//        'expires_at' => 'datetime',
//        'used_at' => 'datetime',
//    ];


    protected array $fillable = [
        'expires_at'
    ];

    public function scopeCanUse($query)
    {
        return $query->whereNull('used_at')->where('expires_at', '>=', now());
    }

    public function markAsUsed()
    {
        $this->used_at = Carbon::now();
        $this->save();
    }


    public function markAsUnused()
    {
        $this->used_at = null;
        $this->save();
    }

    public function coupon()
    {
        return $this->belongsTo(Coupon::class,'id','coupon_id');
    }


    public function device()
    {
        return $this->belongsTo(Device::class,'id','device_id');
    }


}
